1167C - News Distribution - CodeForces Solution


dfs and similar dsu graphs *1400

Please click on ads to support us..

Python Code:


import sys
from collections import deque

toks = (tok for tok in sys.stdin.read().split())



N = int(next(toks))
K = int(next(toks))



adjacency_lists = [[] for _ in range(N)]

for i in range(K):
    group_size = int(next(toks))

    if group_size == 0:
        continue
    if group_size == 1:
        next(toks)
        continue
    
    prev_person = None
    for _ in range(group_size):
                cur_person = int(next(toks))-1

                        if prev_person != None:
            adjacency_lists[prev_person].append(cur_person)
            adjacency_lists[cur_person].append(prev_person)
            
        prev_person = cur_person

comps = []
comp_of_vertex = [None for _ in range(N)]

for vertex in range(N):
            if comp_of_vertex[vertex] == None:
        comps.append([vertex])

                bfs_q = deque()
        bfs_q.append(vertex)
                        comp_of_vertex[vertex] = len(comps)-1

        while len(bfs_q) > 0:
            cur_vertex = bfs_q.popleft()

            for new_vertex in adjacency_lists[cur_vertex]:
                                                                                                if comp_of_vertex[new_vertex] == None:
                    comps[len(comps)-1].append(new_vertex)
                    comp_of_vertex[new_vertex] = len(comps)-1
                    bfs_q.append(new_vertex)
                    
for i in range(N):
    if i > 0:
        sys.stdout.write(" ")
    sys.stdout.write(str(len(comps[comp_of_vertex[i]])))
sys.stdout.write("\n")

 			 				    	  	 	 				      	

C++ Code:

#include<bits/stdc++.h>

using namespace std;

constexpr char ln = '\n';
constexpr int mod = 1e9 + 7;
constexpr long long MxN = 1e18;

int n, m, k;
string s, t;

void run_test_case(){
	cin >> n >> m;

	vector<vector<int>> nodes(n);
	for(int i = 0; i < m; i++){
		cin >> k;
		vector<int> group(k);
		for(int j = 0; j < k; j++)
			cin >> group[j], group[j]--;
		
		for(int j = 0; j + 1 < k; j++){
			nodes[group[j]].push_back(group[j + 1]);
			nodes[group[j + 1]].push_back(group[j]);
		}
	}
	// bfs
	vector<int> cost(n);
	vector<bool> visited(n);
	for(int i = 0; i < n; i++){
		if(visited[i])	continue;

		queue<int> que;
		que.push(i);
		vector<int> current;

		while(!que.empty()){
			int cur = que.front();
			que.pop();
			if(visited[cur])	continue;

			current.push_back(cur);
			visited[cur] = true;

			for(auto node : nodes[cur]){
				if(visited[node])	continue;

				que.push(node);
			}
		}
		for(auto cur : current)
			cost[cur] = current.size();
	}

	for(int i = 0; i < n; i++)
		cout << cost[i] << (i + 1 == n ? "" : " ");
}	

int main(){
	int test = 1;
	// cin >> test;
	while(test--){
		run_test_case();
	}
}
// 


Comments

Submit
0 Comments
More Questions

1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even
1269A - Equation
441A - Valera and Antique Items
1702C - Train and Queries
816B - Karen and Coffee
838D - Airplane Arrangements
148B - Escape
847G - University Classes
1110A - Parity
1215B - The Number of Products
604C - Alternative Thinking
1204C - Anna Svyatoslav and Maps
322A - Ciel and Dancing
1689B - Mystic Permutation
1711B - Party